import { mapMutations, mapActions, mapState } from "vuex";
import _ from "lodash";
const requireComponent = require.context("./", true, /[A-Z]\w+\.(vue)$/);
const comp = {};
requireComponent.keys().forEach((fileName) => {
  if (fileName != "./index.vue") {
    const componentConfig = requireComponent(fileName);
    const name = componentConfig.default.name;
    const componentName = _.camelCase(
      name
        .split("/")
        .pop()
        .replace(/\.\w+$/, "")
    );
    comp[componentName] = componentConfig.default || componentConfig;
  }
});
Object.keys(comp).map((key) => {
  comp[key].components = comp;
});
export default {
  components: comp,
  provide() {
    return {
      anc: this,
    };
  },
  computed: {},
  created() {
    Object.keys(this.models).map((k) => {
      this.models[k].clz = this.$modelObject.create(this.models[k].modelType);
      this.models[k].clzs = this.$modelObject.create(
        this.models[k].modelType + "Collection"
      );
    });
  },
  mounted() {
    this.watchs.map(item=>{
      this.$watch(item.target,item.fn,{deep:item.deep})
    })
    this.$nextTick(() => {
      this.unwatch = this.$watch('vms', (newValue, oldValue) => {
        let flag = true;
        Object.keys(comp).map((k) => {
          if (!Object.keys(this.vms).includes(k)) {
            flag = false;
          }
        });
        this.completeLoad = flag;
      });
      if (process.env.NODE_ENV === "development") {
        this.init();
        console.log({ me: this, vms: this.vms, completeLoad:this.completeLoad });
      }
    });
  },
  destroyed(){
    this.unwatch();
  },
  data() {
    return {
      vms: {}, //页面组件vue实例
      completeLoad: false,
      me:this,
      models: {
        //模型
        consume: {
          modelType: "consume",
        },
        resource: {
          modelType: "resource",
        },
        logic: {
          modelType: "logic",
        },
        staticLogic: {
          modelType: "staticLogic",
        },
        template: {
          modelType: "template",
        },
        twins: {
          modelType: "twins",
        },
        tank: {
          modelType: "tank",
        },
        twins: {
          modelType: "twins",
        },
        workCenter: {
          modelType: "workCenter",
        },
        routing: {
          modelType: "routing",
        },
      },
      dataSet: {
        //数据集
        insList: [],
        classList: [
          { id: "consume", parentId: "root" },
          { id: "resource", parentId: "root" },
          { id: "logic", parentId: "root" },
          { id: "staticLogic", parentId: "root" },
          { id: "template", parentId: "root" },
          { id: "twins", parentId: "root" },
          { id: "tank", parentId: "root" },
          { id: "workCenter", parentId: "root" },
          { id: "routing", parentId: "root" },
        ],
        currentClass: {},
        currentIns: {},
      },
      watchs:[
        {target:'dataSet.currentClass',fn:(val,old)=>{
            this.classListSelect(this.dataSet.currentClass)
        },deep:false}
      ]
    };
  },
  methods: {
    init(){

    },
    classListSelect(item) {
      this.queryIns(this.vms.insList.pageNavigation, {});
    },
    queryIns(pageNavigation, filter, orderBy) {
      this.vms.insList.loading = true;
      this._$queryByPage(
        this.models[this.dataSet.currentClass.id].clzs,
        pageNavigation,
        filter ? filter : {},
        "",
        "",
        false
      ).then((res) => {
        this.dataSet.insList =
          this.models[this.dataSet.currentClass.id].clzs.records;
        this.vms.insList.loading = false;
      });
    },
    showJsonModal(row, index) {
      this.vms.jsonModal.config.show = true;
      this.dataSet.currentIns = row;
    },
  },
};
